package com.itheima.reggie.controller;

import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;
    //http://localhost:8081/user/login
    @PostMapping("/login")
    public ResultInfo login(@RequestBody Map<String,String> map) {
        String phone = map.get("phone");
        User user= userService.findByPhone(phone);
        if (user==null){
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);//保存用户
        }
        if (user.getStatus()==0){
            return ResultInfo.error("用户被禁用,请联系管理员");
        }
        session.setAttribute(Constant.SESSION_USER, user);
        return ResultInfo.success(null);
    }
}
